/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.aspera.vista;

import co.edu.senaclem.logica.ProgramaLogicaLocal;
import co.edu.senaclem.logica.UsuariosLogicaLocal;
import co.edu.senaclem.modelo.Programa;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author DILOVE
 */
@ManagedBean(name = "programaVista")
@RequestScoped
public class ProgramaVista {

    @EJB
    private ProgramaLogicaLocal programaLogica;

    @EJB
    private UsuariosLogicaLocal usuariosLogica;

    private InputText txtCodigo;
    private InputText txtNombre;
    private CommandButton btnRegistrar;
    private CommandButton btnModificar;
    private CommandButton btnEliminar;
    private CommandButton btnLimpiar;
    private Programa selectedPrograma;
    private List<Programa> listaProgramas = null;

    /**
     * @return the txtNombre
     */
    public InputText getTxtNombre() {
        return txtNombre;
    }

    /**
     * @param txtNombre the txtNombre to set
     */
    public void setTxtNombre(InputText txtNombre) {
        this.txtNombre = txtNombre;
    }

    /**
     * @return the txtClave
     */
    /**
     * @return the btnRegistrar
     */
    public CommandButton getBtnRegistrar() {
        return btnRegistrar;
    }

    /**
     * @param btnRegistrar the btnRegistrar to set
     */
    public void setBtnRegistrar(CommandButton btnRegistrar) {
        this.btnRegistrar = btnRegistrar;
    }

    /**
     * @return the btnModificar
     */
    public CommandButton getBtnModificar() {
        return btnModificar;
    }

    /**
     * @param btnModificar the btnModificar to set
     */
    public void setBtnModificar(CommandButton btnModificar) {
        this.btnModificar = btnModificar;
    }

    /**
     * @return the btnEliminar
     */
    public CommandButton getBtnEliminar() {
        return btnEliminar;
    }

    /**
     * @param btnEliminar the btnEliminar to set
     */
    public void setBtnEliminar(CommandButton btnEliminar) {
        this.btnEliminar = btnEliminar;
    }

    /**
     * @return the btnLimpiar
     */
    public CommandButton getBtnLimpiar() {
        return btnLimpiar;
    }

    /**
     * @param btnLimpiar the btnLimpiar to set
     */
    public void setBtnLimpiar(CommandButton btnLimpiar) {
        this.btnLimpiar = btnLimpiar;
    }

    public void registrar_action() {
        //System.out.println("Registrar Programa");
        if (usuariosLogica.validarSesion()) {
            try {
                Programa programa = new Programa();
                Integer codigoPrograma = Integer.parseInt(txtCodigo.getValue().toString());
                programa.setCodigo(codigoPrograma);
                programa.setNombre(txtNombre.getValue().toString().toUpperCase());
                programaLogica.registrar(programa);

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Mensaje: ", "El Programa se Registró con Exito"));
            } catch (Exception e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", e.getMessage()));
            }
            listaProgramas = null;
            limpiar_action();
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../SesionInvalida.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void modificar_action() {
        //System.out.println("Modificar Programa");
        if (usuariosLogica.validarSesion()) {
            try {
                Integer codigoPrograma = Integer.parseInt(txtCodigo.getValue().toString());
                //Modificar
                Programa programa = new Programa();
                programa.setCodigo(codigoPrograma);
                programa.setNombre(txtNombre.getValue().toString().toUpperCase());
                programaLogica.modificar(programa);

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Mensaje: ", "El Programa se Modificó con Exito"));
            } catch (Exception e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", e.getMessage()));
            }
            listaProgramas = null;
            limpiar_action();
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../SesionInvalida.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void borrar_action() {
        //System.out.println("Borrar Programa");
        if (usuariosLogica.validarSesion()) {
            try {
                Integer codigoPrograma = Integer.parseInt(txtCodigo.getValue().toString());
                //Borrar
                Programa programa = new Programa();
                programa.setCodigo(codigoPrograma);
                programaLogica.eliminar(programa);

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Mensaje: ", "El Programa se Eliminó con Exito"));
            } catch (Exception e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", e.getMessage()));
            }
            listaProgramas = null;
            limpiar_action();
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../SesionInvalida.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void limpiar_action() {
        //System.out.println("Limpiar");
        if (usuariosLogica.validarSesion()) {
            txtCodigo.setValue("");
            txtNombre.setValue("");
            btnRegistrar.setDisabled(false);
            btnEliminar.setDisabled(true);
            btnModificar.setDisabled(true);
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../SesionInvalida.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void txtCodigo_listener() {
        //System.out.println("Entro al listener del Codigo del Programa");
        if (usuariosLogica.validarSesion()) {
            Programa pro = null;
            try {
                Integer codigoPrograma = Integer.parseInt(txtCodigo.getValue().toString());
                pro = programaLogica.consultarPorId(codigoPrograma);
            } catch (Exception ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", ex.getMessage()));
            }
            if (pro == null) {
                txtNombre.setValue("");
                btnRegistrar.setDisabled(false);
                btnEliminar.setDisabled(true);
                btnModificar.setDisabled(true);
            } else {
                txtCodigo.setValue(pro.getCodigo());
                txtNombre.setValue(pro.getNombre());
                btnRegistrar.setDisabled(true);
                btnEliminar.setDisabled(false);
                btnModificar.setDisabled(false);
            }
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../SesionInvalida.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void onRowSelect(SelectEvent event) {
        //System.out.println("Selecciono el Programa");
        if (usuariosLogica.validarSesion()) {
            Programa pro = (Programa) event.getObject();
            //System.out.println("Programa Seleccionado"+pro.getNombre());
            //FacesMessage msg = new FacesMessage("Programa Seleccionado", pro.getNombreprograma());
            txtCodigo.setValue(pro.getCodigo());
            txtNombre.setValue(pro.getNombre());
            btnRegistrar.setDisabled(true);
            btnEliminar.setDisabled(false);
            btnModificar.setDisabled(false);
            //FacesContext.getCurrentInstance().addMessage(null, msg); 
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../SesionInvalida.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @return the programaLogica
     */
    public ProgramaLogicaLocal getProgramaLogica() {
        return programaLogica;
    }

    /**
     * @param programaLogica the programaLogica to set
     */
    public void setProgramaLogica(ProgramaLogicaLocal programaLogica) {
        this.programaLogica = programaLogica;
    }

    /**
     * @return the txtCodigo
     */
    public InputText getTxtCodigo() {
        return txtCodigo;
    }

    /**
     * @param txtCodigo the txtCodigo to set
     */
    public void setTxtCodigo(InputText txtCodigo) {
        this.txtCodigo = txtCodigo;
    }

    /**
     * @return the selectedPrograma
     */
    public Programa getSelectedPrograma() {
        return selectedPrograma;
    }

    /**
     * @param selectedPrograma the selectedPrograma to set
     */
    public void setSelectedPrograma(Programa selectedPrograma) {
        this.selectedPrograma = selectedPrograma;
    }

    /**
     * @return the listaProgramas
     */
    public List<Programa> getListaProgramas() {
        if (listaProgramas == null) {
            try {
                //System.out.println("Va a consultar los Programas de Formacion");
                listaProgramas = programaLogica.consultar();
            } catch (Exception ex) {
                Logger.getLogger(ProgramaVista.class.getName()).log(Level.SEVERE, null, ex);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", ex.getMessage()));
            }
        }
        return listaProgramas;
    }

    /**
     * @param listaProgramas the listaProgramas to set
     */
    public void setListaProgramas(List<Programa> listaProgramas) {
        this.listaProgramas = listaProgramas;
    }

    public void onEliminacionConfirm(ActionEvent actionEvent) {
        borrar_action();
    }

}
